package com.example.activitytest.controller;

import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 审批流相关接口
 * @author lxj
 * @since 2023/9/26
 **/
@Slf4j
@RestController
public class ActivitiController {

    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    private HistoryService historyService;

    /**
     * 创建部署流程
     */
    @RequestMapping("/deployProcess")
    public void deployProcess() {
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("test.bpmn20.xml")
                .addClasspathResource("test.png")
                .name("测试流程")
                .deploy();
        log.info("流程部署id = " + deployment.getId());
        log.info("流程部署名称 = " + deployment.getName());
    }

    /**
     * 启动流程实例
     */
    @RequestMapping("/runProcess")
    public void runProcess() {
        Map<String, Object> map = new HashMap<>();
        map.put("flag", "1");
        ProcessInstance instance = runtimeService
                .startProcessInstanceByKey("test2", map);
        log.info("流程实例的名字 = " + instance.getName());
        log.info("流程定义的id = " + instance.getProcessDefinitionId());
        log.info("流程部署的id = " + instance.getDeploymentId());
        log.info("流程实例的id = " + instance.getId());
        log.info("当前活动的id = " + instance.getActivityId());
    }

    /**
     * 查询任务内容
     */
    @RequestMapping("/queryTask/{assignee}")
    public void queryTask(@PathVariable("assignee") String assignee) {
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("test2")
                .taskAssignee(assignee)
                .list();
        for (Task task : list) {
            log.info("流程实例的id = " + task.getProcessInstanceId());
            log.info("任务id = " + task.getId());
            log.info("任务名字 = " + task.getName());
        }
    }

    /**
     * 处理任务
     * @param assignee
     */
    @RequestMapping("/completeTask/{assignee}")
    public void completeTask(@PathVariable("assignee") String assignee) {
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("test")
                .taskAssignee(assignee)
                .list();
        for (Task task : list) {
            taskService.complete(task.getId());
            log.info("完成" + task.getName());
        }
    }

    /**
     * 获取流程信息
     */
    @RequestMapping("/getProcessInfo")
    private void getProcessInfo() throws FileNotFoundException {
        List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("test")
                .list();
        for (ProcessDefinition processDefinition : processDefinitionList) {
            // 获取资源名称，bpmn.xml 文件的名称
            String resourceName = processDefinition.getResourceName();
            // 获取图表资源，bpmn.png 图片的名称
            String diagramResourceName = processDefinition.getDiagramResourceName();
            // 获取资源的输入流
            InputStream resourceNameInputStream = repositoryService
                    .getResourceAsStream(processDefinition.getDeploymentId(), resourceName);
            InputStream diagramResourceNameInputStream = repositoryService
                    .getResourceAsStream(processDefinition.getDeploymentId(), diagramResourceName);
            FileOutputStream xmlOutputStream = new FileOutputStream("D:/test.xml");
            FileOutputStream pngOutputStream = new FileOutputStream("D:/test.png");
            IoUtil.copy(resourceNameInputStream, xmlOutputStream);
            IoUtil.copy(diagramResourceNameInputStream, pngOutputStream);
        }
    }


}
